package com.cl.edu.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.cl.common.CrudActionSupport;
import com.cl.common.util.StringUtil;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Area;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Bookingteacher;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Student;
import com.cl.edu.entity.Teacher;
import com.cl.edu.service.IndexManager;
import com.cl.edu.service.StudentManager;
import com.cl.edu.service.TeacherQueryManager;

@Results( { @Result(name = "qjj", location = "qingjiajiao.jsp") ,
	@Result(name = "studentlist", location = "student-list.jsp"),
	@Result(name = "student", location = "student.jsp")})
public class StudentAction extends CrudActionSupport<Student> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5570012221779895111L;
	private Student entity;
	private Long id;
	private Long teaId;//预约教师ID
	private List<Area>  syList;//区划
	private List<News> fwysList;			//服务优势列表
	private List<News> qjjbzList;			//请家教帮助列表
	private List<News> qjjwtList;			//请家教常见问题列表
	private Page<Student> page=new Page<Student>();//学生列表
	private Page<Bookingstudent> bookList=new Page<Bookingstudent>();//学生列表
	private Page<News> newsPage=new Page<News>();//新闻列表
	private List<News> list	  ;					//栏目列表
	private List<News> list1  ;					//右边栏列表1
	private List<News> list2  ;					//右边栏列表2
	private List<News> list3  ;					//右边栏列表3
	private List<News> list4  ;					//右边栏列表4
	private String filter_id="定单编号";
	private String filter_subject="请输入求教学科";
	private String filter_areaid;
	private String filter_sex;
	private String filter_sexrequire;
	
	private Integer msgType;//消息类型 目前用于请家教时学员填写信息时出提示信息
	@Autowired
	private StudentManager studentManager;
	@Autowired
	private IndexManager indexManager;
	@Autowired
	private TeacherQueryManager teacherQueryManager;
	public String execute()throws Exception{
		syList= studentManager.getShenYang();
		this.initRight();
		return "qjj";
	}
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	//公用右边栏信息
	private void initRight() {
		
		if (!newsPage.isOrderBySetted())		{
			newsPage.setOrderBy("id");
			newsPage.setOrder(Page.DESC);
		}
		newsPage = indexManager.getNewsById(newsPage, 1L, 6);
		list1 = newsPage.getResult();
		
		newsPage = indexManager.getNewsById(newsPage, 13L, 6);
		list2 = newsPage.getResult();
	}
	public String queryOne() throws Exception {
		this.prepareModel();
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");   
			page.setOrder(Page.DESC);
		}
		page=studentManager.getChoiceList(page);
		bookList.setPageSize(3);
		bookList=this.teacherQueryManager.getBookingStudent(bookList, id);
		this.initRight();
		return "student";
	}
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		syList= studentManager.getShenYang();
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page=studentManager.getChoiceList(page);
		this.initRight();
		return "studentlist";
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id == null) {
			entity = new Student();
		} else {
			entity = studentManager.getStudentById(id);
		}
	}

	@Override
	public String save() throws Exception {
		if(!StringUtil.isEmpty(entity.getName())){
			entity.setState(1);//未经审核
			entity.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			studentManager.save(entity);
		}
		//Struts2Utils.getRequest().setAttribute("message", "请家教信息发布成功，请等待工作人员与您联系。");
		msgType=1;
		if(this.teaId!=null){//保存预约信息
			Bookingteacher bt=new Bookingteacher();
			bt.setStudent(entity);
			bt.setTeacher(teacherQueryManager.getTeacherById(teaId));
			bt.setBookTime(new Date());
			bt.setState(0);
			studentManager.saveBookingMessage(bt);
			msgType=2;
			//Struts2Utils.getRequest().setAttribute("message", "预约教师信息发布成功，请等待工作人员与您联系。");
		}
		Struts2Utils.getResponse().sendRedirect("/student!list.action?msgType="+msgType);
		return null;
		//return list();
	}
	public String bookingStudent()throws Exception{
		if(teacherQueryManager.checkBooked(teaId, id)){
			Struts2Utils.getRequest().setAttribute("message", "您已预约过该学员，请等待工作人员与您联系。");
			return list();
		}
		this.prepareModel();
		Bookingstudent bs=new Bookingstudent();
		bs.setStudent(this.entity);
		Teacher temp=teacherQueryManager.getTeacherById(teaId);
		if(temp.getState()!=0){
			Struts2Utils.getRequest().setAttribute("message", "您的账号尚未通过审核，不能预约学员。");
			return list();
		}
		bs.setTeacher(temp);
		bs.setBookTime(new Date());
		bs.setState(0);
		studentManager.saveBookingStudentMessage(bs);
		Struts2Utils.getRequest().setAttribute("message", "预约学员成功，请等待工作人员与您联系。");
		return list();
	}

	public Student getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	public void setEntity(Student entity) {
		this.entity = entity;
	}

	public Student getEntity() {
		return entity;
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public void setSyList(List<Area> syList) {
		this.syList = syList;
	}
	public List<Area> getSyList() {
		return syList;
	}
	public void setPage(Page<Student> page) {
		this.page = page;
	}
	public Page<Student> getPage() {
		return page;
	}
	public String getFilter_id() {
		return filter_id;
	}
	public void setFilter_id(String filter_id) {
		this.filter_id = filter_id;
	}
	public String getFilter_subject() {
		return filter_subject;
	}
	public void setFilter_subject(String filter_subject) {
		this.filter_subject = filter_subject;
	}
	public String getFilter_areaid() {
		return filter_areaid;
	}
	public void setFilter_areaid(String filter_areaid) {
		this.filter_areaid = filter_areaid;
	}
	public String getFilter_sex() {
		return filter_sex;
	}
	public void setFilter_sex(String filter_sex) {
		this.filter_sex = filter_sex;
	}
	public String getFilter_sexrequire() {
		return filter_sexrequire;
	}
	public void setFilter_sexrequire(String filter_sexrequire) {
		this.filter_sexrequire = filter_sexrequire;
	}
	public List<News> getFwysList() {
		return fwysList;
	}
	public void setFwysList(List<News> fwysList) {
		this.fwysList = fwysList;
	}
	public List<News> getQjjbzList() {
		return qjjbzList;
	}
	public void setQjjbzList(List<News> qjjbzList) {
		this.qjjbzList = qjjbzList;
	}
	public List<News> getQjjwtList() {
		return qjjwtList;
	}
	public void setQjjwtList(List<News> qjjwtList) {
		this.qjjwtList = qjjwtList;
	}
	public List<News> getList() {
		return list;
	}
	public void setList(List<News> list) {
		this.list = list;
	}
	public List<News> getList1() {
		return list1;
	}
	public void setList1(List<News> list1) {
		this.list1 = list1;
	}
	public List<News> getList2() {
		return list2;
	}
	public void setList2(List<News> list2) {
		this.list2 = list2;
	}
	public List<News> getList3() {
		return list3;
	}
	public void setList3(List<News> list3) {
		this.list3 = list3;
	}
	public List<News> getList4() {
		return list4;
	}
	public void setList4(List<News> list4) {
		this.list4 = list4;
	}
	public void setTeaId(Long teaId) {
		this.teaId = teaId;
	}
	public Long getTeaId() {
		return teaId;
	}
	public void setBookList(Page<Bookingstudent> bookList) {
		this.bookList = bookList;
	}
	public Page<Bookingstudent> getBookList() {
		return bookList;
	}
	public Integer getMsgType() {
		return msgType;
	}
	public void setMsgType(Integer msgType) {
		this.msgType = msgType;
	}
	
}
